home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / apilot.lha / APilot / APilot_Ser / misc.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-12-03  |  6.0 KB  |  260 lines

  1. /***************************************************************************
  2.  *
  3.  * misc.c -- Some misc routines..
  4.  *
  5.  *-------------------------------------------------------------------------
  6.  * Authors: Casper Gripenberg  (casper@alpha.hut.fi)
  7.  *          Kjetil Jacobsen  (kjetilja@stud.cs.uit.no)
  8.  *
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <math.h>
  13. #include <exec/types.h>
  14. #include <intuition/intuition.h>
  15. #include <proto/graphics.h>
  16.  
  17. #include "map.h"
  18. #include "defs.h"
  19. #include "misc.h"
  20. #include "ships.h"
  21.  
  22. #ifndef PURE_OS
  23. #include "hline_protos.h"
  24. #endif
  25.  
  26. /*--------------------------------------------------------------------------*/
  27.  
  28. int sina[360];
  29. int cosa[360];
  30. int atana[PRECS+1];
  31.  
  32. extern AWorld World;
  33.  
  34. int pixelbyterow[WP_TABLESIZE];        /* Need this in HorizontalLine */
  35. #ifndef PURE_OS
  36. static int pixelbytecol[WP_TABLESIZE];
  37. #endif
  38.  
  39. /*--------------------------------------------------------------------------*/
  40.  
  41. /*
  42.  * init_sctables -- Precalculates sine & cosine tables for fast lookup +
  43.  *                  arctan table upto 45 degrees.
  44.  *
  45.  */
  46. void
  47. init_sctables(void)
  48. {
  49.   int i;
  50.  
  51.   for (i = 0; i < 360; i++) {
  52.     sina[i] = (int) (sin((double)(PI/180)*(double)i) * PRECS);
  53.     cosa[i] = (int) (cos((double)(PI/180)*(double)i) * PRECS);
  54.   }
  55.   for (i = 0; i < PRECS+1; i++)
  56.     atana[i] = (int) (atan((double)i/(double)PRECS)*180.0/PI);
  57. }
  58.  
  59. /*--------------------------------------------------------------------------*/
  60.  
  61. #ifndef PURE_OS    /* We don't need these on 3rd party cards.. */
  62.  
  63. /*
  64.  * init_writepixel -- Precalculates some pixel values, speeds up
  65.  *                    myWritePixel with about 30%.
  66.  */
  67. void
  68. init_writepixel( int bytesPerRow )
  69. {
  70.   int i;
  71.  
  72.   for (i = 0; i < WP_TABLESIZE; i++) {
  73.     pixelbyterow[i] = i * bytesPerRow;
  74.     pixelbytecol[i] = (i >> 3);
  75.   }
  76. }  
  77.  
  78. /*--------------------------------------------------------------------------*/
  79.  
  80. /*
  81.  * myWritePixel -- Just a speedier WritePixel routine than the one provided
  82.  *                 by the system. (Does no range/window checking).
  83.  *                 int mode: 1 -> Set the pixel, 0 -> Clear it.
  84.  */
  85. void
  86. myWritePixel( PLANEPTR thePlane, int x, int y, int mode )
  87. {
  88.   int pixbyte, pixrot;
  89.  
  90.   /* 
  91.    * pixbyte = y*bytesPerRow + (x >> 3); 
  92.    */
  93.  
  94.   pixbyte = pixelbyterow[y] + pixelbytecol[x];
  95.   pixrot  = x & 0x7;
  96.  
  97.   if (mode == 1)
  98.     thePlane[pixbyte] |= (0x80 >> pixrot);
  99.   else
  100.     thePlane[pixbyte] &= ~(0x80 >> pixrot);
  101. }
  102.  
  103. /*--------------------------------------------------------------------------*/
  104.  
  105. /*
  106.  * VerticalLine -- Draws...gasp...vertical lines.
  107.  *
  108.  */
  109. void
  110. VerticalLine( PLANEPTR thePlane, int x, int y, int length, int onoff )
  111. {
  112.   int i, pixbyte;
  113.   USHORT pixbit;
  114.   int bpr = pixelbyterow[1];
  115.  
  116.   pixbyte = pixelbyterow[y] + pixelbytecol[x];
  117.   pixbit  = (onoff == 0) ? ~(0x80 >> (x & 0x7)) : (0x80 >> (x & 0x7)) ;
  118.   
  119.   if (onoff == 0) {
  120.     for (i = 0; i < length; i++) {
  121.       thePlane[pixbyte] &= pixbit;
  122.       pixbyte += bpr;
  123.     }
  124.   } else {
  125.     for (i = 0; i < length; i++) {
  126.       thePlane[pixbyte] |= pixbit;
  127.       pixbyte += bpr;
  128.     }
  129.   }
  130. }
  131.  
  132. #endif /* PURE_OS */
  133.  
  134. /*--------------------------------------------------------------------------*/
  135.  
  136. /*
  137.  * draw_hud -- Draws the heads up display on the local console.
  138.  *             To be expanded...
  139.  */
  140. void
  141. draw_hud( struct RastPort *rp, UWORD buf, UWORD nframes )
  142. {
  143.   /* 
  144.    * h_drawn is used when erasing the hud; tells in which buffers 
  145.    * the hud has been drawn. 
  146.    */
  147.   static int  ontime   = 0;
  148.   static BOOL h_drawn[MY_BUFFERS] = { FALSE, FALSE };
  149.  
  150.   AShip    *lship = World.local_ship;
  151.   char cbuf[10];
  152.   int  i, fuelheight;
  153.   int  sv_x, sv_y;
  154. #ifndef PURE_OS
  155.   PLANEPTR bpl1;
  156.  
  157.   bpl1 = rp->BitMap->Planes[1];
  158. #endif
  159.  
  160.   /*
  161.    * Remove old speedvector..
  162.    */
  163.   SetAPen(rp, 0);
  164.   SetWriteMask(rp, 1l);
  165.   Move(rp, LOCL_X, LOCL_Y);
  166.   Draw(rp, World.p_sv[buf].x, World.p_sv[buf].y);
  167.   
  168.   /*
  169.    * Draw hud upper and lower lines..
  170.    */
  171. #ifdef PURE_OS
  172.   SetWriteMask(rp, 2l);
  173.   SetAPen(rp,2);
  174.   for (i = 0; i < 10; i++) {
  175.     HLINE( rp, LOCL_X-63+(13*i), LOCL_Y-51, 10 )
  176.   }
  177.   HLINE( rp, LOCL_X-63, LOCL_Y+51, 128 )
  178. #else
  179.   for (i = 0; i < 10; i++) {
  180.     HorizontalLine( bpl1, LOCL_X-63+(13*i), LOCL_Y-51, 10, 1 );
  181.   }
  182.   HorizontalLine( bpl1, LOCL_X-63, LOCL_Y+51, 128, 1 );
  183. #endif
  184.  
  185.   if (World.local_ship->fueling || World.hudon) {
  186.     World.hudon = FALSE;
  187.     ontime = 1;
  188.   }
  189.  
  190.   /*
  191.    * Draw fuelmeter..
  192.    */
  193.   if (ontime != 0) {
  194.     ontime += nframes;
  195.     if (ontime >= HUDON_TIME) {
  196.       ontime = 0;
  197.     }
  198. #ifdef PURE_OS
  199.     SetAPen(rp,2);
  200.     HLINE( rp, LOCL_X+54, LOCL_Y-48, 10 )
  201.     HLINE( rp, LOCL_X+54, LOCL_Y+37, 10 )
  202.     VLINE( rp, LOCL_X+54, LOCL_Y-47, 84 )
  203.     VLINE( rp, LOCL_X+63, LOCL_Y-47, 84 )
  204. #else
  205.     HorizontalLine( bpl1, LOCL_X+54, LOCL_Y-48, 10, 1 );
  206.     HorizontalLine( bpl1, LOCL_X+54, LOCL_Y+37, 10, 1 );
  207.     VerticalLine( bpl1, LOCL_X+54, LOCL_Y-47, 84, 1 );
  208.     VerticalLine( bpl1, LOCL_X+63, LOCL_Y-47, 84, 1 );
  209. #endif
  210.     fuelheight = ((((World.local_ship->fuel << SHFTPR) / MAXFUEL) * 82)
  211.                  >> SHFTPR);
  212. #ifndef PURE_OS
  213.     SetWriteMask(rp, 2l);
  214. #endif
  215.  
  216.     SetAPen(rp, 0);
  217.     RectFill( rp, LOCL_X+56, LOCL_Y-47+fuelheight, 
  218.                   LOCL_X+61, LOCL_Y+35 );
  219.     SetAPen(rp, 2);
  220.     RectFill( rp, LOCL_X+56, LOCL_Y+35-fuelheight, 
  221.                   LOCL_X+61, LOCL_Y+35 );
  222.  
  223.     sprintf(cbuf, "%5d", World.local_ship->fuel);
  224.     Move( rp, LOCL_X+39, LOCL_Y+47 );
  225.     Text( rp, cbuf, 5 );
  226.  
  227.     h_drawn[buf] = TRUE;
  228.  
  229.   } else if (h_drawn[buf] == TRUE) {
  230.     /*
  231.      * Remove fuelmeter..
  232.      */
  233.     SetAPen(rp, 0);
  234.     SetWriteMask(rp, 2l);
  235.     RectFill( rp, LOCL_X+54, LOCL_Y-48, LOCL_X+63, LOCL_Y+37 );
  236.     RectFill( rp, LOCL_X+38, LOCL_Y+40, LOCL_X+63, LOCL_Y+48 );
  237.     h_drawn[buf] = FALSE;
  238.   }  
  239.  
  240.   /*
  241.    * Check if dead.
  242.    */
  243.   if (lship->status > 0)
  244.     return;
  245.  
  246.   /*
  247.    * Calculate and draw new speed vector.
  248.    */
  249.   sv_x = LOCL_X + (((lship->xvel * 20) >> SHFTPR) % LOCL_X);
  250.   sv_y = LOCL_Y + (((lship->yvel * 20) >> SHFTPR) % LOCL_Y);
  251.  
  252.   World.p_sv[buf].x = sv_x;
  253.   World.p_sv[buf].y = sv_y;
  254.  
  255.   SetAPen(rp, 1);
  256.   SetWriteMask(rp, 1l);
  257.   Move(rp, LOCL_X, LOCL_Y);
  258.   Draw(rp, sv_x, sv_y);
  259. }
  260.